# join 等待,主进程等待子进程结束之后,在执行.

# from multiprocessing import Process
# import time
#
#
# def task(name):
#
#     print(f'{name} is running')
#
#
#
# if __name__ == '__main__':
#
#     p = Process(target=task,args=('李业',))
#     p.start()
#     time.sleep(1)
#     print('===主进程')


# 上面的版本,虽然达到目的,但是生产环境中,子进程结束的时间不定. 需要用到join

# from multiprocessing import Process
# import time
#
#
# def task(name):
#     time.sleep(1)
#     print(f'{name} is running')
#
#
#
# if __name__ == '__main__':
#
#     p = Process(target=task,args=('李业',))
#     p.start()
#     p.join()  # 告知主进程,p进程结束之后,主进程在运行.
#     # join感觉有一些阻塞的意思.
#     print('===主进程')


# 开启多个子进程去验证:
# from multiprocessing import Process
# import time
#
#
# def task(name):
#     time.sleep(1)
#     print(f'{name} is running')
#
#
#
# if __name__ == '__main__':
#
#     p1 = Process(target=task, args=('李业',))
#     p2 = Process(target=task, args=('怼哥',))
#     p3 = Process(target=task, args=('mc骚Q',))
#
#     p1.start()
#     p2.start()
#     p3.start()
#     # p1, p2, p3 三个子进程运行的先后顺序不定.
#     # start只是通知一下操作系统,三个start几乎同一时刻发给操作系统,
#     # 操作系统调用cpu先运行谁,谁先执行.
#
#     # print('===主进程')


# 下面的版本验证了: 如此写join并不是串行.
# from multiprocessing import Process
# import time
#
#
# def task(name,sec):
#     time.sleep(sec)
#     print(f'{name} is running')
#
#
# if __name__ == '__main__':
#     p1 = Process(target=task, args=('李业', 1))
#     p2 = Process(target=task, args=('怼哥', 2))
#     p3 = Process(target=task, args=('mc骚Q', 3))
#     start_time = time.time()
#     p1.start()
#     p2.start()
#     p3.start()
#     # p1, p2, p3 三个子进程运行的先后顺序不定.
#     # start只是通知一下操作系统,三个start几乎同一时刻发给操作系统,
#     # 操作系统调用cpu先运行谁,谁先执行.
#     p1.join()  # 阻塞不在这. time.sleep(1)
#     p2.join()
#     p3.join()
#     print(f'===主进程:{time.time() - start_time}之后,执行')

#
# from multiprocessing import Process
# import time
#
#
# def task(name,sec):
#     time.sleep(sec)
#     print(f'{name} is running')
#
#
# if __name__ == '__main__':
#     p1 = Process(target=task, args=('李业', 1))
#     p2 = Process(target=task, args=('怼哥', 1))
#     p3 = Process(target=task, args=('mc骚Q', 1))
#     start_time = time.time()
#     p1.start()
#     p2.start()
#     p3.start()
#     # p1, p2, p3 三个子进程运行的先后顺序不定.
#     # start只是通知一下操作系统,三个start几乎同一时刻发给操作系统,
#     # 操作系统调用cpu先运行谁,谁先执行.
#     p1.join()
#     print(f'===主进程:{time.time() - start_time}之后,执行')


# 下面的结果是什么,为什么!



# from multiprocessing import Process
# import time
#
#
# def task(name,sec):
#
#     print(f'{name} is running')
#     time.sleep(sec)
#     print(f'{name} is done')
#
#
#
# if __name__ == '__main__':
#     p1 = Process(target=task, args=('李业', 3))
#     # p2 = Process(target=task, args=('怼哥', 2))
#     # p3 = Process(target=task, args=('mc骚Q', 3))
#     start_time = time.time()
#     p1.start()
#     p1.join()
#     # print(111)
#     p2.start()
#     p2.join()
#     p3.start()
#     p3.join()
#
#     print(f'===主进程:{time.time() - start_time}之后,执行')


#
# from multiprocessing import Process
# import time
#
#
# def task(name,sec):
#
#     print(f'{name} is running')
#     time.sleep(sec)
#     print(f'{name} is done')
#
#
#
# if __name__ == '__main__':
#     p1 = Process(target=task, args=('李业', 3))
#     # p2 = Process(target=task, args=('怼哥', 2))
#     # p3 = Process(target=task, args=('mc骚Q', 3))
#     start_time = time.time()
#     p1.start()
#     p1.join()
#     p2.start()
#     p2.join()
#     p3.start()
#     p3.join()
#
#     print(f'===主进程:{time.time() - start_time}之后,执行')


# from multiprocessing import Process
# import time
#
#
# def task(name,sec):
#     time.sleep(sec)
#     print(f'{name} is running')
#
#
# if __name__ == '__main__':
#     p1 = Process(target=task, args=('李业', 3))
#     p2 = Process(target=task, args=('怼哥', 2))
#     p3 = Process(target=task, args=('mc骚Q', 1))
#     start_time = time.time()
#     p1.start()
#     p2.start()
#     p3.start()
#     # p1, p2, p3 三个子进程运行的先后顺序不定.
#     # start只是通知一下操作系统,三个start几乎同一时刻发给操作系统,
#     # 操作系统调用cpu先运行谁,谁先执行.
#     p1.join()
#     p2.join()
#     p3.join()
#     print(f'===主进程:{time.time() - start_time}之后,执行')




# from multiprocessing import Process
# import time
#
#
# def task(sec):
#     time.sleep(sec)
#     print(f'{sec}: is running')
#
#
# if __name__ == '__main__':
#     # p1 = Process(target=task, args=(1,))
#     # p2 = Process(target=task, args=(2,))
#     # p3 = Process(target=task, args=(3,))
#     # start_time = time.time()
#     # p1.start()
#     # p2.start()
#     # p3.start()
#     start_time = time.time()
#     p_l = []
#     for i in range(1,4):
#         p = Process(target=task, args=(i,))
#         p.start()
#         p_l.append(p)
#
#     for i in p_l:
#         i.join()
#
#
#     print(f'===主进程:{time.time() - start_time}之后,执行')

